package com.uniriho.lxr0811_7300_bl.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import com.uniriho.lxr0811_7300_bl.base.Constant;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;

public class packetHandles {
    private static byte[] encrypt = new byte[1];
    

    /**
     * @param cmdCode   指令：  例如 01，02
     * @param isEncrypt 是否加密 false
     * @param pnum      包数  默认：1
     * @param pindex    包序号  默认：1
     * @param data      内容
     * @return
     *
     */
    public static byte[] packetHandles(String cmdCode, boolean isEncrypt, int pnum, int pindex,
                                byte[] data) {
        Log.i("zsm", "*********协议封包*********");
        try {
            int dataLen = 0;
            if (data != null) {
                dataLen = data.length;
            }
            Log.i("zsm", "数据长度：" + dataLen);
            int allLen = 15 + dataLen;
            Log.i("zsm", "总包长度：" + allLen);
            byte[] sendData = new byte[allLen];
            int indexLen = 0;
            byte[] head = ByteAndHexUtil.hexToByteArray("FF00FF");
            Log.i("zsm", "包头：" + ByteAndHexUtil.bytesToHex(head));
            System.arraycopy(head, 0, sendData, indexLen, head.length);

            indexLen += head.length;
            byte[] packetLen = ByteAndHexUtil.intToTwoByte(allLen);
            Log.i("zsm", "包长度：" + ByteAndHexUtil.bytesToHex(packetLen));
            System.arraycopy(packetLen, 0, sendData, indexLen, packetLen.length);

            indexLen += packetLen.length;
            byte[] type = ByteAndHexUtil.hexToByteArray("01");
            Log.i("zsm", "数据类型：" + ByteAndHexUtil.bytesToHex(type));
            System.arraycopy(type, 0, sendData, indexLen, type.length);

            indexLen += type.length;
            byte[] cmd = ByteAndHexUtil.hexToByteArray(cmdCode);
            Log.i("zsm", "指令：" + ByteAndHexUtil.bytesToHex(cmd));
            System.arraycopy(cmd, 0, sendData, indexLen, cmd.length);

            indexLen += cmd.length;
            if (isEncrypt) {
                encrypt[0] = 0x01;
            } else {
                encrypt[0] = 0x00;
            }
            Log.i("zsm", "加密：" + ByteAndHexUtil.bytesToHex(encrypt));
            System.arraycopy(encrypt, 0, sendData, indexLen, encrypt.length);


            byte[] pNum = ByteAndHexUtil.intToTwoByte(pnum);
            Log.i("zsm", "分包长度：" + ByteAndHexUtil.bytesToHex(pNum));
            indexLen += encrypt.length;
            System.arraycopy(pNum, 0, sendData, indexLen, pNum.length);

            byte[] pIndex = ByteAndHexUtil.intToTwoByte(pindex);
            Log.i("zsm", "分包序号：" + ByteAndHexUtil.bytesToHex(pIndex));
            indexLen += pNum.length;
            System.arraycopy(pIndex, 0, sendData, indexLen, pIndex.length);

            indexLen += pIndex.length;
            int crcLen = 9;
            if (dataLen > 0) {
                System.arraycopy(data, 0, sendData, indexLen, data.length);
                Log.i("zsm", "内容：" + ByteAndHexUtil.bytesToHex(data));
            }
            crcLen += dataLen;
            byte[] crcData = new byte[crcLen];
            System.arraycopy(sendData, 3, crcData, 0, crcLen);
            Log.i("zsm", "CRC校验内容:" + ByteAndHexUtil.bytesToHex(crcData));

            int crctoInt = ByteAndHexUtil.CRC16_XMODEMtoInt(crcData);
            String crcRes = String.format("%04X", crctoInt);
            Log.i("zsm", "CRC校验码:" + crcRes);
            byte[] crc = ByteAndHexUtil.hexToByteArray(crcRes);
            Log.i("zsm", "CRC校验码长度:" + crc.length);
            Log.i("zsm", "CRC校验码:" + ByteAndHexUtil.bytesToHex(crc));

            indexLen += dataLen;
            System.arraycopy(crc, 0, sendData, indexLen, crc.length);


            byte[] pEnd = ByteAndHexUtil.hexToByteArray("FE");
            Log.i("zsm", "包尾:" + ByteAndHexUtil.bytesToHex(pEnd));
            indexLen += crc.length;
            System.arraycopy(pEnd, 0, sendData, indexLen, pEnd.length);
            Log.i("zsm", "完整数据:" + ByteAndHexUtil.bytesToHex(sendData));
            return sendData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * FF00FF 0011 01 71 000001000103002F1DFE
     * 数据解析 获取设备时间实例包
     * // 包头：FF00FF
     * // 长度：0020
     * // 类型：02
     * // 指令：01
     * // 标识：00
     * // 包数：0001
     * // 序号：0001
     * // 状态：00
     * // 内容：32346131636337393631303433326538==20231110151601
     * // 校验：528C
     * // 包尾：FE
     *
     * @param parseData
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String packeParse(String parseData) {
        boolean type = true;
        String parseResult = "";
        String content16 = "";
        String state = "失败";
        String cmd = parseData.substring(12, 14);
        System.out.println("cmd==>" + cmd);
        String res = parseData.substring(24, 26);
        if (res.equals("00")) {
            state = "成功";
            content16 = parseData.substring(26, parseData.length() - 6);
            System.out.println("content16==>" + content16);
            parseResult = ByteAndHexUtil.convertHexToASC(content16);
            String string = new String(content16);
            System.out.println("contentStr==>" + parseResult);
            System.out.println("string==>" + string);
        }
        String description = "";
        switch (cmd) {
            case "01":
                description = "获取CPUID:" + state + "\nCPUID:";
                break;
            case "02":
                description = "读固件版本:" + state + "\n固件版本：";
                break;
            case "03":
                description = "写入设备ID:" + state + "\n设备ID：";
                break;
            case "04":
                description = "设置设备时间:" + state + "\n设置时间：";
                break;
            case "05":
                description = "读设备时间:" + state + "\n设备时间：";
                break;
            case "11":
                description = "Sam上电:" + state;
                break;
            case "12":
                description = "Sam复位:" + state;
                break;
            case "13":
                description = "读随机数:" + state+"\n随机数:";
                break;
            case "14":
                type = false;
                description = "加密：" + state+"\n加密数据:";
                break;
            case "15":
                type = false;
                description = "解密：" + state+"\n解密数据：";
                break;
            case "16":
                description = "sam卡老化测试：" + state+"\n:";
                break;
            case "20":
                description = "指纹上下电：" + state;
                break;
            case "21":
                description = "读产品序列号：" + state+"\n序列号:";
                break;
            case "22":
                description = "读安全等级：" + state+"\n安全等级:";
                break;
            case "23":
                description = "设置安全等级：" + state;
                break;
            case "24":
                description = "采集指纹：" + state;
                break;
            case "25":
                description = "合成模板：" + state;
                break;
            case "26":
                type = false;
                description = "指纹对比：" + state;
                break;
            case "27":
                description = "清除模板：" + state;
                break;
            case "28":
                description = "：指纹上传" + state;
                break;
            case "29":
                description = "下载指纹：" + state;
                break;
            case "40":
                type = false;
                description = "超高频交互：" + state+"\n:交互数据：";
                break;
            case "41":
                description = "读模块版本：" + state+"\n模块版本:";
                break;
            case "42":
                type = false;
                description = "寻标签：" + state+"\n标签ID:";
                content16 = content16.substring(2,content16.length());
                Constant.BQID = content16;
                break;
            case "43":
                type = false;
                description = "读标签：" + state+"\n标签内容:";
                break;
            case "44":
                description = "写标签：" + state;
                break;
            case "45":
                type = false;
                description = "读功率：" + state+"\n功率:";
                int toTen = ByteAndHexUtil.toTen(content16);
                content16 = toTen+"DB";
                break;
            case "46":
                description = "设功率：" + state;
                break;
            case "47":
                description = "读频带：" + state+"\n频带:";
                break;
            case "48":
                description = "设频带：" + state;
                break;
            case "49":
                description = "读天线：" + state+"\n天线号:";
                break;
            case "4A":
                description = "设天线：" + state;
                break;
            case "4B":
                description = "UHF老化：" + state;
                break;
            case "61":
                description = "语音测试：" + state;
                break;
            case "62":
                description = "写音频文件：" + state;
                break;
            case "63":
                description = "替换音频：" + state;
                break;
            case "71":
                description = "LED控制：" + state;
                break;
            case "81":
                description = "设置超时："+state;
                break;
            case "82":
                description = "读设置超时：" + state+"\n超时：";
                break;

        }
        if (type) {
            return description+parseResult;
        }else{
            return description+content16;
        }
    }

    public static void main(String[] args) {
        String testData = "FF00FF001E02050000010001003230323331313130313435393133888AFE";
        String packeParse = packeParse(testData);
        System.out.println("packeParse==>" + packeParse);
    }

}
